"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:SignIndexCtrl
 * @description
 * # SignIndexCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller(
    "SignIndexCtrl",
    function (
      $location,
      localStorageService,
      $interval,
      MapService,
      Util,
      DataService,
      openApi
    ) {
      var vm = this;
      vm.param = {};
      // 默认地址坐标
      vm.relList = [
        { key: "无", value: "无" },
        { key: "投标申请单", value: "bid_apply" },
        { key: "工程合同", value: "contract_apply" },
        { key: "外出办事", value: "go_out" },
        { key: "出差", value: "travel_apply" },
      ];
      vm.chooseinfo = function () {
        if (!vm.param.relatedModule || vm.param.relatedModule == "无") {
          Util.Toast("请选择关联模块");
          return;
        }
        // 里面包含了title
        var relModuleObj = {
          selectTab: vm.param.relatedModule,
          selectTabContent: vm.param.relatedModuleName,
        };
        localStorageService.set("signparam", vm.param);
        Util.savaDataforsearch(relModuleObj);
        $location.path("finance/choosepeople/-1");
      };
      vm.modulechange = function () {
        vm.param.relatedModuleName = Util.getkeyFromValue(
          vm.relList,
          vm.param.relatedModule
        );
        vm.param.moduleNo = "";
      };
      // 打卡
      vm.sign = function () {
        if (vm.hassigned) {
          Util.Toast("已经签到过了");
          return;
        }
        var param = {};
        if (!vm.param.point) {
          Util.Toast("请获取当前位置信息");
          return;
        }
        if (vm.param.relatedModule && vm.param.relatedModule !== "无") {
          if (!vm.param.moduleNo) {
            Util.Toast("请选择模块编号");
            return;
          }
        }

        param = angular.copy(vm.param, {});
        param.lat = vm.param.point.latitude;
        param.lng = vm.param.point.longitude;
        // 传的坐标不一样
        delete param.point;
        console.log(vm.param);
        DataService.getData(openApi.signin, param).then(function (data) {
          console.log(data);
          if (data.code == 1) {
            Util.Toast(data.result, { icon: "qdcg.png" });
            vm.curData.signInCount++;
            vm.hassigned = true;
            vm.param.relatedModule = "";
            vm.param.moduleNo = "";
            vm.param.remark = "";
            vm.param.attachment = "";
          } else {
            Util.Toast(data.result);
          }
        });
      };
      vm.goFootmark = function () {
        $location.path("sign/footmarklist").replace();
      };
      vm.chooseposition = function () {
        localStorageService.set("signparam", vm.param);
        $location.path("sign/chooselocation");
      };
      vm.getPosition = function () {
        vm.param.address = "";
        vm.param.title = "";
        vm.positiontip = "定位中..";
        MapService.getcurPositon().then(
          function (data) {
            vm.param.point = {};
            vm.param.point.latitude = data.latitude;
            vm.param.point.longitude = data.longitude;
            vm.param.address =
              data.address +
              (data.describe || "") +
              (data.title ? "(" + data.title + ")" : "");
            vm.positiontip = "";
            // 更新map信息
            vm.point.latitude = data.latitude;
            vm.point.longitude = data.longitude;
            vm.mapOptions.markers = [vm.point];
          },
          function (data) {
            vm.positiontip = "定位失败，请重新定位";
          }
        );
      };

      // 获取位置
      var getPosition = function () {
        if (!vm.param.point) {
          vm.getPosition();
        } else {
          vm.point.latitude = vm.param.point.latitude;
          vm.point.longitude = vm.param.point.longitude;
          vm.mapOptions.markers = [vm.point];
        }
      };
      // 获取当前考勤信息
      var getSigndata = function () {
        DataService.getData(openApi.signinStatus).then(function (data) {
          vm.curData = data;
        });
      };
      var initDate = function () {
        vm.signDate = new Date();
        $interval(function () {
          vm.signDate = new Date();
        }, 1000);
      };
      vm.point = {
        longitude: 115.863834,
        latitude: 28.68003,
      };
      this.ak = MapService.getAk();
      // 初始化地图
      var initMap = function () {
        vm.offlineOpts = { retryInterval: 5000 };
        vm.mapOptions = {
          center: vm.point,
          zoom: 15,
          overviewCtrl: false,
          navCtrl: false, //删除缩放控件
          scaleCtrl: false, //删除比例尺控件  ,
          city: "NanChang",
          enableDragging: false,
        };
      };
      // 初始化成功后将map和标记返回以便获取具体位置
      this.finishmap = function (map, previousMarkers) {
        vm.map = map;
        vm.previousMarkers = previousMarkers;
        vm.map.removeControl(vm.map.scaleControl);
        // 获取当前位置
        getPosition();
      };
      var getData = function () {
        vm.param = localStorageService.get("signparam") || {};
        localStorageService.remove("signparam");
        var selectData;
        selectData = Util.getDataforsearch();
        vm.param = angular.extend(vm.param, selectData.filterData);
        console.log(vm.param);
      };
      var init = function () {
        getData();
        getSigndata();
        initDate();
        initMap();
        $interval(function () {
          if (!vm.param.point) {
            vm.getPosition();
          }
        }, 3000);
      };

      init();
    }
  );
